package com.clouduserservice8001.service.impl;

import com.clouduserservice8001.entity.Employee;
import com.clouduserservice8001.dto.EmployeeVO;
import com.clouduserservice8001.mapper.EmployeeMapper;
import com.clouduserservice8001.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 员工服务实现类
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Override
    public boolean createEmployee(Employee employee, String createBy) {
        // 检查员工工号是否已存在
        if (employee.getEmployeeNo() != null && isEmployeeNoExists(employee.getEmployeeNo())) {
            return false;
        }
        
        // 如果没有提供员工工号，自动生成
        if (employee.getEmployeeNo() == null || employee.getEmployeeNo().isEmpty()) {
            employee.setEmployeeNo(generateEmployeeNo());
        }
        
        // 设置创建信息
        employee.setCreateBy(createBy);
        employee.setCreateTime(LocalDateTime.now());
        employee.setStatus("1"); // 默认在职
        
        return employeeMapper.insert(employee) > 0;
    }
    
    @Override
    public boolean updateEmployee(Employee employee, String updateBy) {
        // 先根据userId查询现有员工信息，获取id字段
        Employee existingEmployee = employeeMapper.selectByUserId(employee.getUserId());
        if (existingEmployee == null) {
            return false; // 员工不存在
        }

        // 设置主键ID
        employee.setId(existingEmployee.getId());

        // 设置更新信息
        employee.setUpdateBy(updateBy);
        employee.setUpdateTime(LocalDateTime.now());

        // 使用专门的更新方法，避免字段不匹配问题
        return employeeMapper.updateEmployeeSelective(employee) > 0;
    }
    
    @Override
    public boolean deleteEmployee(String userId, String updateBy) {
        // 逻辑删除：设置状态为离职
        return updateEmployeeStatus(userId, "0", updateBy);
    }
    
    @Override
    public boolean updateEmployeeStatus(String userId, String status, String updateBy) {
        return employeeMapper.updateStatus(userId, status, updateBy, LocalDateTime.now()) > 0;
    }
    
    @Override
    public Employee getEmployeeByUserId(String userId) {
        return employeeMapper.selectByUserId(userId);
    }
    
    @Override
    public Employee getEmployeeByEmployeeNo(String employeeNo) {
        return employeeMapper.selectByEmployeeNo(employeeNo);
    }
    
    @Override
    public List<Employee> getEmployeesByDept(String deptName) {
        return employeeMapper.selectByDeptName(deptName);
    }
    
    @Override
    public List<Employee> getAllEmployees() {
        return employeeMapper.selectAll();
    }
    
    @Override
    public List<Employee> getEmployeeList(String deptName, String status, String keyword) {
        return employeeMapper.selectEmployeeList(deptName, status, keyword);
    }
    
    @Override
    public List<Employee> getEmployeesByManager(String managerId) {
        return employeeMapper.selectByManagerId(managerId);
    }
    
    @Override
    public boolean isEmployeeNoExists(String employeeNo) {
        return employeeMapper.countByEmployeeNo(employeeNo) > 0;
    }
    
    @Override
    public String generateEmployeeNo() {
        // 生成员工工号：EMP + 年月日 + 时分秒
        // 例如：EMP20250106143025
        LocalDateTime now = LocalDateTime.now();
        String timestamp = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String employeeNo = "EMP" + timestamp;
        
        // 如果工号已存在，添加随机数
        int counter = 1;
        String finalEmployeeNo = employeeNo;
        while (isEmployeeNoExists(finalEmployeeNo)) {
            finalEmployeeNo = employeeNo + String.format("%02d", counter);
            counter++;
        }
        
        return finalEmployeeNo;
    }

    // ===== 以下是EmployeeVO相关方法的实现 =====

    @Override
    public EmployeeVO getEmployeeVOByUserId(String userId) {
        return employeeMapper.selectEmployeeVOByUserId(userId);
    }

    @Override
    public EmployeeVO getEmployeeVOByEmployeeNo(String employeeNo) {
        return employeeMapper.selectEmployeeVOByEmployeeNo(employeeNo);
    }

    @Override
    public List<EmployeeVO> getEmployeeVOList(String deptName, String status, String keyword) {
        return employeeMapper.selectEmployeeVOList(deptName, status, keyword);
    }

    @Override
    public List<EmployeeVO> getEmployeeVOsByDept(String deptName) {
        return employeeMapper.selectEmployeeVOByDept(deptName);
    }

    @Override
    public List<EmployeeVO> getEmployeeVOsByManager(String managerId) {
        return employeeMapper.selectEmployeeVOByManager(managerId);
    }
}
